# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>

_realname=OpenBLAS
pkgbase=mingw-w64-openblas
pkgname=("${MINGW_PACKAGE_PREFIX}-openblas"
         $([[ "${CARCH}" == "i686" ]] || echo "${MINGW_PACKAGE_PREFIX}-openblas64"))
pkgver=0.3.23
pkgrel=1
pkgdesc="An optimized BLAS library based on GotoBLAS2 1.13 BSD, providing optimized blas, lapack, and cblas (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.openblas.net/"
license=('spdx:BSD-3-Clause')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-openblas-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-${_realname}-git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-fc")
             "dos2unix")
options=('!buildflags')
source=(https://github.com/xianyi/OpenBLAS/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz
        002-lgfortran-requires-lquadmath.patch
        004-aarch64-detection.patch
        005-fix-pkgconfig-files.patch)
sha256sums=('5d9491d07168a5d00116cdc068a40022c3455bf9293c7cb86a65b1054d7e5114'
            '465057bcaaaec93d671681f0c5e79b8634bf5c9aca4f495338780ada2dffd49c'
            '70ce007ff4f6c2a127f9835ce9cd85081707cf3bcccddcfaa06817b06f69c941'
            'bb36b75624fe9768d3d16e9d824e79d4dea5b0f0056601ca4ce37bc93e46f4b5')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    002-lgfortran-requires-lquadmath.patch \
    004-aarch64-detection.patch \
    005-fix-pkgconfig-files.patch
}

_build_openblas() {
  _idx_opt=$1

  declare _build_type
  if check_option "debug" "n"; then
    _build_type="Release"
  else
    _build_type="Debug"
  fi

  declare _c_lapack_opt
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _c_lapack_opt="-DC_LAPACK=ON"
  fi

  declare target=CORE2
  declare dynamic_arch="-DDYNAMIC_ARCH=ON"
  if [[ ${MINGW_PACKAGE_PREFIX} == *-aarch64 ]]; then
    target=CORTEXA53
    # clang wasn't able to compile all the kernels inside the arm64 folder,
    # so disable for now
    dynamic_arch="-DDYNAMIC_ARCH=OFF"
  fi

  unset CFLAGS
  unset CXXFLAGS
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    export CFLAGS=" -Wno-maybe-uninitialized"
  else
    export CFLAGS=" -Wno-unused-function -Wno-unused-variable"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_build_type} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_STATIC_LIBS=ON \
    ${dynamic_arch} \
    -DUSE_THREAD=ON \
    -DNUM_THREADS=64 \
    -DUSE_OPENMP=ON \
    -DTARGET=${target} \
    ${_c_lapack_opt} \
    ${_idx_opt} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}-32" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-32"
  mkdir -p "${srcdir}/build-${MSYSTEM}-32" && cd "${srcdir}/build-${MSYSTEM}-32"

  msg2 "Build OpenBLAS with 32-bit indexing"
  _build_openblas ""

  if [ "${CARCH}" != "i686" ]; then
    [[ -d "${srcdir}/build-${MSYSTEM}-64" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-64"
    mkdir -p "${srcdir}/build-${MSYSTEM}-64" && cd "${srcdir}/build-${MSYSTEM}-64"

    msg2 "Build OpenBLAS with 64-bit indexing"
    _build_openblas "-DBINARY=64 -DINTERFACE64=1"
  fi
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}-32
  ctest

  if [ "${CARCH}" != "i686" ]; then
    cd "${srcdir}/build-${MSYSTEM}-64"
    ctest
  fi
}

package_openblas() {
  pkgdesc="An optimized BLAS library based on GotoBLAS2 1.13 BSD, providing optimized blas, lapack, and cblas (mingw-w64)"

  cd "${srcdir}"/build-${MSYSTEM}-32

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  # Install License
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/lapack-netlib/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-lapack
}

package_openblas64() {
  pkgdesc="An optimized BLAS library based on GotoBLAS2 1.13 BSD, providing optimized blas, lapack, and cblas with 64-bit indexing (mingw-w64)"
  provides=()
  conflicts=()
  replaces=()

  cd "${srcdir}"/build-${MSYSTEM}-64

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  # Install License
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}64/LICENSE
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/lapack-netlib/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}64/LICENSE-lapack
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
